package dao

import (
	"context"
	"gorm.io/gorm"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
	"ucenter/model"
)

type UserPaidTrackDao struct {
	conn *gorms.GormConn
}

func NewUserPaidTrackDao(db *db.ListenDB) *UserPaidTrackDao {
	return &UserPaidTrackDao{
		conn: gorms.New(db.Conn),
	}
}
func (u *UserPaidTrackDao) FindPaidTrack(ctx context.Context, albumId int64) (paidTracks []*model.UserPaidTrack, err error) {
	session := u.conn.Session(ctx)
	// 查找已经购买的音频
	err = session.Model(&model.UserPaidTrack{}).Where("album_id = ?", albumId).Find(&paidTracks).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return paidTracks, nil

}
func (u *UserPaidTrackDao) FindPaidTrackByUserId(ctx context.Context, userId int64, trackId int64) (paidTracks []*model.UserPaidTrack, err error) {
	session := u.conn.Session(ctx)
	// 查找已经购买的音频
	err = session.Model(&model.UserPaidTrack{}).Where("user_id = ? and album_id = ?", userId, trackId).Find(&paidTracks).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return paidTracks, nil
}
